<template>
    <div class="wrapper">
        <div class="content">
            <div class="user">
                <div class="avatar">
                    <img
                        :src="mztUser.picture || Avatar"
                        alt="头像"
                    />
                    <img
                        v-if="mztUser.sex === '0'"
                        class="sex"
                        src="../../assets/images/icon-female.png"
                        alt="性别"
                    />
                    <img
                        v-if="mztUser.sex === '1'"
                        class="sex"
                        src="../../assets/images/icon-male.png"
                        alt="性别"
                    />
                </div>
                <div class="info">
                    <p class="name">{{ mztUser.name | nameFilter }}</p>
                    <p>身份证号: {{ mztUser.cardNum | IDFilter }}</p>
                </div>
            </div>
            <div
                class="authorization"
                @click="handleTo('authorization')"
            >
                <div>
                    <p class="label">
                        <img
                            src="../../assets/images/icon-authorization.png"
                            alt=""
                        />
                        <span>有效授权</span>
                    </p>
                    <p class="val">{{ validAuth }}</p>
                </div>
                <div class="line"></div>
                <div>
                    <p class="label">
                        <img
                            src="../../assets/images/icon-authorization2.png"
                            alt=""
                        />
                        <span>累计授权</span>
                    </p>
                    <p class="val">{{ cumulAuth }}</p>
                </div>
            </div>
            <div class="enter">
                <ul>
                    <li @click="personalAuthorization">
                        <img
                            src="../../assets/images/authorization-personal.png"
                            alt="个人数据授权icon"
                        />
                        <p>个人数据授权</p>
                    </li>
                    <li @click="enterpriseAuthorization">
                        <img
                            src="../../assets/images/authorization-enterprise.png"
                            alt="企业数据授权icon"
                        />
                        <p>企业数据授权</p>
                    </li>
                </ul>
            </div>
            <div
                v-if="hasScan"
                class="qr-scan"
                @click="handleScan"
            >
                扫一扫
            </div>
        </div>
        <div class="home-footer">
            <p>主办：福建省大数据管理局</p>
            <p>技术支持：数字中国研究院（福建）</p>
            <p class="tip">
                服务热线：
                <a href="tel:0591-87820626">0591-87820626</a>
            </p>
        </div>
        <Popup
            ref="popup"
            @on-confirm="handleOk"
        />
    </div>
</template>

<script>
import Avatar from '@/assets/images/avatar.jpg';
import Popup from './popup';
import { restful_api } from '@/api';
export default {
    name: 'Home',
    components: {
        Popup,
    },
    filters: {
        nameFilter: function (val) {
            if (!val) return '';
            if (val.length < 4) return '*' + val.substr(1);
            else return '**' + val.substr(2);
        },
        IDFilter: function (val) {
            if (!val) return '';
            let len = val.length - 4;
            return new Array(len).fill('*').join('') + val.substr(len);
        },
    },
    data() {
        return {
            hasScan: false,
            Avatar,

            cumulAuth: 0,
            validAuth: 0,
        };
    },
    async mounted() {
        this.main();
    },
    methods: {
        handleScan() {
            app.barcode.scan(function (res) {
                location.href = res;
            });
        },
        handleOk() {
            this.$refs.popup.close();
        },
        // 机构授权
        async enterpriseAuthorization() {
            let data = {
                methodId: 'TP350003',
                params: {
                    certCode: this.mztUser.cardNum,
                },
            };
            // 根据身份证号码查询名下企业
            let res = restful_api(data);
            if (res.list && res.list.length > 0) {
                this.handleTo('apply', 2);
            } else {
                this.$refs.popup.open();
            }
        },
        // 个人授权
        personalAuthorization() {
            this.handleTo('apply', 1);
        },
        // 根据身份证号码判断该的授权情况
        async inquireAuthorization() {
            let data = {
                methodId: 'TP350004',
                params: {
                    certCode: this.mztUser.cardNum,
                },
            };
            let res = await restful_api(data);
            this.cumulAuth = res.cumulAuth;
            this.validAuth = res.validAuth;
        },
        async main() {
            let fns = this.$route.query.fn || this.$utils.getUrlParams('fn');
            fns = fns && fns.split(',');
            this.hasScan = fns.includes('scan');
            // 根据身份证号码判断该的授权情况
            this.inquireAuthorization();
        },
        handleTo(nav, userType) {
            this.$router.push({ path: nav, query: { userType } });
        },
    },
};
</script>
<style scoped>
.wrapper {
    background: #f5f5f5 url('../../assets/images/background.png') no-repeat;
    background-size: 100% auto;
}
.content {
    padding: 30px 16px 0;
}
.user {
    display: flex;
    margin-bottom: 32px;
}
.avatar {
    position: relative;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    box-sizing: border-box;
    margin-right: 16px;
    margin-left: 2px;
}
.avatar img {
    width: 100%;
    border-radius: 50%;
}
.avatar .sex {
    position: absolute;
    right: -2px;
    bottom: -2px;
    width: 20px;
    height: 20px;
}
.info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    font-size: 13px;
    line-height: 26px;
    color: #fff;
}
.info .name {
    font-size: 20px;
    line-height: 30px;
}
.authorization {
    display: flex;
    align-items: center;
    width: 100%;
    height: 70px;
    border-radius: 10px;
    margin-bottom: 12px;
    font-size: 13px;
    line-height: 26px;
    text-align: center;
    color: #999;
    background-color: #fff;
}

.authorization > div:not([class]) {
    display: flex;
    flex-direction: column;
    flex: 1;
    justify-content: center;
}
.authorization > div.line {
    width: 1px;
    height: 40px;
    border-left: solid thin #efefef;
}
.authorization > div .label {
    display: flex;
    justify-content: center;
    align-items: center;
}
.authorization > div .label img {
    width: 10px;
    /* height: 14px; */
    margin-right: 5px;
}
.authorization > div .val {
    font-size: 16px;
    color: #000;
}
.enter ul {
    display: flex;
    justify-content: space-between;
    margin: 0 -6px;
    text-align: center;
}
.enter ul li {
    flex: 1;
    height: 170px;
    border-radius: 10px;
    margin: 0 6px;
    background-color: #fff;
}
.enter ul li img {
    width: 160px;
    margin: 10px 0 15px;
}
.qr-scan {
    width: 100%;
    height: 63px;
    margin-top: 13px;
    color: #fff;
    font-size: 16px;
    line-height: 56px;
    text-align: center;
    background: url(~@/assets/images/home-btn.png) no-repeat;
    background-size: contain;
}
.home-footer {
    position: fixed;
    bottom: 0;
    width: 100vw;
    padding-bottom: 12px;
    font-size: 10px;
    line-height: 16px;
    text-align: center;
    color: #999;
}

.home-footer .tip {
    color: #1e6fd9;
}
</style>
